package kr.or.kead.module;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import kr.or.kead.service.PostService;

@SuppressWarnings("serial")
public class PostGetAddr extends JDialog implements ActionListener {
	private JPanel panel;
	private JTextField txtDong;
	private JButton btnSearch;
	private JPanel panel_1;
	private JButton bntOk;
	private JButton btnCancel;
	private JScrollPane scrollPane;
	private JList<String> list;
	private JPanel panel_2;
	private JTextField txtMainAddr;
	private JTextField txtRemainAddr;
	private JTextField parentAddr;
			
	public PostGetAddr(JTextField addr) {
		setTitle("주소 검색");
		parentAddr = addr;
		initialize();
	}
	
	private void initialize() {
		getContentPane().setLayout(new BorderLayout(5, 5));
		
		panel = new JPanel();
		getContentPane().add(panel, BorderLayout.NORTH);
		
		txtDong = new JTextField();
		panel.add(txtDong);
		txtDong.setColumns(10);
		
		btnSearch = new JButton("검색");
		btnSearch.addActionListener(this);
		panel.add(btnSearch);
		
		panel_1 = new JPanel();
		getContentPane().add(panel_1, BorderLayout.SOUTH);
		
		bntOk = new JButton("확인");
		bntOk.addActionListener(this);
		panel_1.add(bntOk);
		
		btnCancel = new JButton("취소");
		btnCancel.addActionListener(this);
		panel_1.add(btnCancel);
		
		panel_2 = new JPanel();
		getContentPane().add(panel_2, BorderLayout.CENTER);
		panel_2.setLayout(new BoxLayout(panel_2, BoxLayout.Y_AXIS));
		
		scrollPane = new JScrollPane();
		panel_2.add(scrollPane);
		
		list = new JList<>();
		list.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {				
				super.mouseClicked(e);
				txtMainAddr.setText(list.getSelectedValue());
				txtRemainAddr.setSelectionStart(0);				
			}			
		});
		scrollPane.setViewportView(list);
		
		txtMainAddr = new JTextField();		
		panel_2.add(txtMainAddr);
		txtMainAddr.setColumns(10);
		
		txtRemainAddr = new JTextField();
		txtRemainAddr.setText("나머지 상세 주소 입력");
		txtRemainAddr.addFocusListener(new FocusAdapter() {
			public void focusGained(FocusEvent e) {
				super.focusGained(e);
				txtRemainAddr.setText("");				
			}			
		});
		panel_2.add(txtRemainAddr);
		txtRemainAddr.setColumns(10);
		
		pack();
	}

	
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnCancel) {
			btnCancelActionPerformed(e);
		}else if (e.getSource() == bntOk) {
			bntOkActionPerformed(e);
		}else if (e.getSource() == btnSearch) {
			btnSearchActionPerformed(e);
		}
	}
	
	protected void btnSearchActionPerformed(ActionEvent e) {		
		ArrayList<String> addrs = new PostService().selectedAdderByDong(txtDong.getText().trim());
		if(addrs.size() == 0 ) {
			JOptionPane.showMessageDialog(null, "해당 되는 주소를 찾을 수 없습니다.");
			txtDong.setSelectionStart(0);
			txtDong.requestFocus();
		}else {
			DefaultListModel<String> model = new DefaultListModel<>();
			for(String data:addrs) {			
				model.addElement(data);
			}
			list.setModel(model);
			list.updateUI();			
		}		
	}
	
	protected void bntOkActionPerformed(ActionEvent e) {
		String result = txtMainAddr.getText() + " " + txtRemainAddr.getText();
		parentAddr.setText(result);
		dispose();
	}
	
	protected void btnCancelActionPerformed(ActionEvent e) {
		dispose();
	}
}
